﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Categories : System.Web.UI.Page
{
	private string cs = System.Configuration.ConfigurationManager.ConnectionStrings["Resist"].ConnectionString;
	
	protected void Page_Load(object sender, EventArgs e)
    {
		LoadCats();
		
		if (Request.Url.AbsoluteUri.Contains("?"))
			try { LoadCat(Request.Url.AbsoluteUri.Substring(Request.Url.AbsoluteUri.LastIndexOf('?') + 1)); }
			catch { pnlCat.Visible = false; }
    }
	private void LoadCats()
	{
		string lbl = "<table style=\"width: 100%;\"><tr>";
		DataTable dt = KC.DataAccess.Global.Global.CallSql_Query(cs, "select c.ID, c.Name, c.FriendlyName, c.Description, count(p.ID) posts from categories c" +
			"	inner join postcategorymap m on c.id=m.categoryid " +
			"	inner join Posts p on m.PostID=p.ID " +
			"where p.isEnabled = 1 " +
			"group by c.ID, c.Name, c.FriendlyName, c.Description order by c.Name");
		for (int i = 0; i < dt.Rows.Count; i++) {
			lbl += "<td><a href=\"Categories.aspx?" + dt.Rows[i]["FriendlyName"].ToString() + "\" title=\"" + Server.HtmlEncode(dt.Rows[i]["Description"].ToString()) + "\">" + dt.Rows[i]["Name"].ToString() + "</a> ("+dt.Rows[i]["Posts"].ToString()+")</td>";
			if (i % 6 == 0) lbl += "</tr>\r\n<tr>"; }
		lbl += "</tr></table>";
		lblCats.Text = lbl;
	}
	private void LoadCat(string fname)
	{
		DataTable dtCat = KC.DataAccess.Global.Global.CallSql_Query(cs, "select * from Categories where FriendlyName='" + fname.Replace("'", "''") + "'");
		DataTable dtPosts = KC.DataAccess.Global.Global.CallSql_Query(cs, "select * from Posts where isEnabled=1 and ID in (select PostID from PostCategoryMap where CategoryID = " + dtCat.Rows[0][0].ToString() + ")");
		pnlCat.Visible = dtPosts.Rows.Count > 0;
		rPosts.DataSource = dtPosts;
		rPosts.DataBind();
		lblCat.Text = Server.HtmlEncode(dtCat.Rows[0]["Name"].ToString());
	}
	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
		DataRowView dr = (DataRowView)e.Item.DataItem;
		((HyperLink)e.Item.FindControl("hlTitle")).Text = Server.HtmlEncode(dr["Title"].ToString());
		((HyperLink)e.Item.FindControl("hlTitle")).NavigateUrl = "http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString();
		((Label)e.Item.FindControl("lblDate")).Text = ((DateTime)dr["Created"]).ToString("MM/dd/yyyy hh:mm ttt");
		string body = common.StripTagsCharArray(dr["Body"].ToString());
		if (body.Length > 1024) body = body.Substring(0, 1018) + "<a href=\"http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString() + "\" alt=\"Continue Reading \"> [...] </a>";
		((Label)e.Item.FindControl("lblBody")).Text = body;

		// Load "Posted In" category list
		DataTable dtCats = KC.DataAccess.Global.Global.CallSql_Query(cs, "select * from Categories where ID in (select CategoryID from PostCategoryMap where PostID=" + dr["ID"].ToString() + ") order by Name");
		string cats = "";
		foreach (DataRow cat in dtCats.Rows)
			cats += "<a href=\"Categories.aspx?" + cat["FriendlyName"].ToString() + "\" Title=\"" + Server.HtmlEncode(cat["Description"].ToString()) + "\">" + cat["Name"].ToString() + "</a>, ";
		((Label)e.Item.FindControl("lblCats")).Text = cats.TrimEnd(new char[] { ',', ' ' });
	}
}